home *** CD-ROM | disk | FTP | other *** search
- Path: iconet.hongkong.net!wong.yuk.wah%f18.n1000.z128
- From: wong.yuk.wah%f18.n1000.z128@iconet.hongkong.net (Wong Yuk Wah)
- Newsgroups: comp.lang.c
- Subject: Re: Date Arithmetic
- Distribution: world
- Message-ID: <56c8e9f056c8e9f0@iconet.hongkong.net>
- Date: Tue, 20 Feb 1996 22:34:00 HKT
- Organization: IcoNET (HONG KONG) <--> Internet gateway
- X-Mailer: MailGate 0.25+
- Reply-To: Wong Yuk Wah <Wong.Yuk.Wah%f18.n1000.z128@iconet.hongkong.net>
-
- At 19:01:50, on 15-02-96, mkaras@pclink.com wrote about Date Arithmetic.
-
- I've worked out a program and hope it would be useful. This program inputs
- a date (today), and the number of days to be added (n), and outputs the
- date of n days later.
-
-
- (Cut)---------------------------------------------------------------------
-
-
- /* Date Arithmetic */
- #include <stdio.h>
-
- struct date {
- int day, month, year;
- };
-
- typedef struct date Date;
-
- void add_day(Date *, int);
-
- void main(void)
- {
- Date today;
- int n;
-
- /* Inputs today's date and no. of days to be added */
- printf("Enter today's date (dd-mm-yyyy): ");
- scanf("%i%*c%i%*c%i", &today.day, &today.month, &today.year);
- printf("Enter the number of days to add: ");
- scanf("%d", &n);
-
- printf("Original date: %d-%d-%d\n", today.day, today.month, today.year);
- add_day(&today, n);
- printf("New date: %d-%d-%d\n", today.day, today.month, today.year);
- }
-
- /* Function that adds no_of_days to Date d */
- void add_day(Date *d, int no_of_days)
- {
- Date temp; /* Date for temporary storage during calculation */
- int days_of_this_month, days_of_next_month, days_to_be_added,
- days_of_month[13] = {0, 31, 28, 31, 30, 31, 30,
- 31, 31, 30, 31, 30, 31};
-
- temp.day = d->day;
- temp.month = d->month;
- temp.year = d->year;
-
- while (no_of_days > 0) {
- if (temp.month == 2)
- days_of_this_month = temp.year % 4 == 0 &&
- (temp.year % 100 != 0 || temp.year % 400 == 0)
- ? 29 : 28;
- else
- days_of_this_month = days_of_month[temp.month];
-
- if (temp.day < days_of_this_month) {
- days_to_be_added = no_of_days < days_of_this_month - temp.day
- ? no_of_days : days_of_this_month - temp.day;
- no_of_days -= days_to_be_added;
- temp.day += days_to_be_added;
- }
- else {
- temp.month++;
-
- if (temp.month == 2)
- days_of_next_month = temp.year % 4 == 0 &&
- (temp.year % 100 != 0 ||
- temp.year % 400 == 0) ? 29 : 28;
- else
- days_of_next_month = days_of_month[temp.month];
-
- days_to_be_added = no_of_days < days_of_next_month
- ? no_of_days : days_of_next_month;
- no_of_days -= days_to_be_added;
- temp.day = days_to_be_added;
- }
- }
-
- d->day = temp.day;
- d->month = temp.month;
- d->year = temp.year;
- }
-
-
- (Cut)--------------------------------------------------------------------
-
-
- Please note that if n is too large, the performance would be
- unacceptable. This is because the calculation is based on month-by-month
- approach. If n is sufficiently large, you may modify the program to
- perform addition year by year. This shouldn't be a problem since the
- algorithm is similar.
-
- There is another better algorithm -- stores the whole date as an int (or
- unsigned int) as most operating systems do. Addition and subtraction can
- thus be performed easily. But the problem is how to represent the date
- efficiently.
-
- That's all. Hope the above text is helpful. Bye!
-
-
- Regards,
- Wong Yuk Wah
-
- * Origin: IcoNET <--> Internet/Usenet gateway, 128:1000/1 (128:1000/1)
-